package com.example.hzzrpc.config.registrycenter;

import com.example.hzzrpc.config.annotations.RpcService;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * @author ：hzz
 * @description：TODO
 * @date ：2022/1/2 11:42
 */
public class RpcServerRegistrarBeanPostProcesser  implements BeanPostProcessor {




    private RpcServerContext    rpcServerContext;


    public RpcServerRegistrarBeanPostProcesser(RpcServerContext rpcServerContext) {
        this.rpcServerContext = rpcServerContext;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        RpcService annotation = bean.getClass().getAnnotation(RpcService.class);
        if(annotation!=null) {
            String serverName = annotation.ServerName();
            if (serverName == null || "".equalsIgnoreCase(serverName.trim())) {
                serverName = bean.getClass().getInterfaces()[0].getName();
            }
            rpcServerContext.addRpcServer(serverName, beanName);
        }
        return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
    }


}
